home *** CD-ROM | disk | FTP | other *** search
/ Windows Undocumented File Formats / Windows Undocumented File Formats.img / CHAP1 / SETVAL / SETVAL.C < prev   
C/C++ Source or Header  |  1997-07-20  |  2KB  |  74 lines

  1. /**********************************************************************
  2.  *
  3.  * PROGRAM: SETVAL.C
  4.  *
  5.  * PURPOSE: Modify a byte in a file
  6.  *
  7.  * Copyright 1997, Mike Wallace and Pete Davis
  8.  *
  9.  * Chapter 1, Introduction and Overview, from Undocumented Windows
  10.  * File Formats, published by R&D Books, an imprint of Miller Freeman, Inc.
  11.  *
  12.  **********************************************************************/
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. void usage()
  18. {
  19.   printf("Usage: SETVAL fn addr val\n");
  20.   printf("  Where addr is the hex location of the byte\n");
  21.   printf("  in the file to modify and val is the byte (in hex)\n");
  22.   printf("  to set at that location.\n\n");
  23.   printf(" Example:  SETVAL LIST.TXT 4B3 FF\n\n");
  24. }
  25.  
  26. int main(int argc, char *argv[])
  27. {
  28.   char filename[256];
  29.   char address[10], val[10];
  30.   long nAddr = 0, nVal = 0;
  31.   FILE *fp;
  32.  
  33.   if (argc != 4)
  34.   {
  35.     usage();
  36.     return 0;
  37.   }
  38.   strcpy(filename, argv[1]);
  39.  
  40.   if ((fp = fopen(filename, "r+b")) == NULL)
  41.   {
  42.     printf("Bad filename supplied\n");
  43.     return 0;
  44.   }
  45.  
  46.   strcpy(address, argv[2]);
  47.   strcpy(val, argv[3]);
  48.  
  49.   sscanf(address, "%lx", &nAddr);
  50.   sscanf(val, "%lx", &nVal);
  51.  
  52.   if (nVal < 0 || nVal > 255)
  53.   {
  54.     usage();
  55.     printf("Error: Supply a val between 0 and 255\n");
  56.   }
  57.  
  58.   fseek(fp, 0, SEEK_END);
  59.   if (ftell(fp) < nAddr)
  60.   {
  61.     printf("The address supplied is beyond the end of this file.\n");
  62.     printf("Please supply a value within the scope of this file.\n");
  63.     fclose(fp);
  64.     return 0;
  65.   }
  66.  
  67.   fseek(fp, nAddr + 1, SEEK_SET);
  68.   fwrite(&nVal, 1, 1, fp);
  69.   fclose(fp);
  70.  
  71.   printf("File modified successfully.\n");
  72.   return 0;
  73. }
  74.